package com.ddzj.scaffold.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Author:yangzibo
 * @CreateTime:2024-04-11 16:00
 * @Description:
 * @Version:1.0
 **/
@Component
public class AuditMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        boolean createDate = metaObject.hasSetter("createTime");
        boolean updateDate = metaObject.hasSetter("lastUpdateTime");
        if (createDate || updateDate) {
            LocalDateTime now = LocalDateTime.now();
            if (createDate) {
                this.strictInsertFill(metaObject, "createTime", () -> now, LocalDateTime.class); //

            }
            if (updateDate) {
                this.strictInsertFill(metaObject, "lastUpdateTime", () -> now, LocalDateTime.class); //
            }
        }
        if (metaObject.hasSetter("createAccount")) {
            this.strictInsertFill(metaObject, "createAccount", () -> "", String.class); //
        }

        if (metaObject.hasSetter("lastUpdateAccount")) {
            this.strictInsertFill(metaObject, "lastUpdateAccount", () -> "", String.class); //
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.hasSetter("lastUpdateTime")) {
            this.strictUpdateFill(metaObject, "lastUpdateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        }
        if (metaObject.hasSetter("lastUpdateAccount")) {
            this.strictUpdateFill(metaObject, "lastUpdateAccount", () -> "", String.class);
        }
    }

}
